<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 虚拟内存机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% - 12px);
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    Redis 虚拟内存机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    深入探索 Redis 如何通过虚拟内存技术突破物理内存限制，实现大规模数据存储的优雅解决方案
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-memory text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">内存优化</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-hdd text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">磁盘扩展</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-exchange-alt text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">智能交换</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- Introduction -->
        <section class="bg-white rounded-2xl shadow-xl p-8 md:p-12 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">R</span>edis 的 VM（虚拟内存）机制是一个用于处理内存管理和优化的大型数据存储系统中的重要功能。Redis 的 VM 机制旨在解决内存使用的问题，特别是在数据集较大且超出可用内存时。虽然 Redis 在早期版本中引入了 VM 机制，但在 Redis 4.0 及以上版本中，它已被替代为更高效的持久化机制和内存管理策略。
            </p>
        </section>

        <!-- Overview Section -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 section-title">一、Redis 的 VM 机制概述</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-bullseye"></i>
                    </div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">1. 目的</h3>
                    <p class="text-gray-600 leading-relaxed">
                        Redis 的 VM 机制的主要目的是将部分数据从内存迁移到磁盘，以便处理大于物理内存的数据集。这样可以让 Redis 在物理内存有限的情况下处理更多的数据，从而提高系统的可扩展性和灵活性。
                    </p>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-cogs"></i>
                    </div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">2. 原理</h3>
                    <p class="text-gray-600 leading-relaxed mb-4">
                        Redis 的 VM 机制的基本原理包括以下几个方面：
                    </p>
                    <div class="space-y-3">
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                            <div>
                                <strong class="text-gray-800">虚拟内存分页</strong>
                                <p class="text-gray-600 text-sm mt-1">将 Redis 数据集划分为小块（称为页面或页），这些页面存储在磁盘上，而不是全部加载到内存中。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                            <div>
                                <strong class="text-gray-800">懒加载</strong>
                                <p class="text-gray-600 text-sm mt-1">只有在需要访问数据时，Redis 才将相关的页面从磁盘加载到内存中。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                            <div>
                                <strong class="text-gray-800">交换策略</strong>
                                <p class="text-gray-600 text-sm mt-1">当内存不足时，Redis 将数据从内存交换到磁盘，以释放内存空间。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Working Principle Visualization -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 section-title">二、工作原理</h2>
            
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8">
                <h3 class="text-xl font-semibold mb-6 text-center text-gray-800">Redis VM 工作流程图</h3>
                <div class="mermaid">
                    graph TD
                        A[客户端请求] --> B{数据在内存中?}
                        B -->|是| C[直接返回数据]
                        B -->|否| D[从磁盘加载页面]
                        D --> E[将页面载入内存]
                        E --> F[返回数据]
                        G[内存不足] --> H[选择页面交换]
                        H --> I[将页面写入磁盘]
                        I --> J[释放内存空间]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                        style G fill:#f56565,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-xl p-6">
                    <h4 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-database text-purple-600 mr-2"></i>
                        1. 数据存储和加载
                    </h4>
                    <div class="space-y-3 text-gray-600">
                        <p><strong>数据存储：</strong>Redis 将数据分为多个页面，并将这些页面存储在磁盘中。</p>
                        <p><strong>数据访问：</strong>当客户端请求数据时，Redis 首先检查数据是否在内存中。</p>
                    </div>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-xl p-6">
                    <h4 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-file-alt text-blue-600 mr-2"></i>
                        2. 页面管理
                    </h4>
                    <div class="space-y-3 text-gray-600">
                        <p><strong>页面文件：</strong>Redis 将每个页面存储为一个磁盘文件。</p>
                        <p><strong>页面表：</strong>Redis 维护一个页面表，跟踪页面位置。</p>
                    </div>
                </div>
                
                <div class="bg-gradient-to-br from-green-50 to-teal-50 rounded-xl p-6">
                    <h4 class="text-xl font-semibold mb-4 text-gray-800">
                        <i class="fas fa-memory text-green-600 mr-2"></i>
                        3. 内存管理
                    </h4>
                    <div class="space-y-3 text-gray-600">
                        <p><strong>页面替换：</strong>根据 LRU、LFU 等策略进行页面交换。</p>
                        <p><strong>写操作：</strong>将修改的数据写入内存并同步到磁盘。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Pros and Cons -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 section-title">三、优缺点分析</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-gradient-to-br from-green-500 to-green-600 text-white rounded-xl shadow-lg p-8">
                    <h3 class="text-2xl font-semibold mb-6 flex items-center">
                        <i class="fas fa-thumbs-up mr-3"></i>
                        优点
                    </h3>
                    <div class="space-y-4">
                        <div class="bg-white bg-opacity-20 rounded-lg p-4">
                            <h4 class="font-semibold mb-2">扩展